package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.enums.BorrowAuthEnum;
import com.atguigu.srb.core.enums.BorrowerStatusEnum;
import com.atguigu.srb.core.enums.IntegralEnum;
import com.atguigu.srb.core.mapper.BorrowerAttachMapper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.mapper.UserIntegralMapper;
import com.atguigu.srb.core.pojo.entity.Borrower;
import com.atguigu.srb.core.mapper.BorrowerMapper;
import com.atguigu.srb.core.pojo.entity.BorrowerAttach;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.entity.UserIntegral;
import com.atguigu.srb.core.pojo.vo.BorrowerApprovalVO;
import com.atguigu.srb.core.pojo.vo.BorrowerAttachVO;
import com.atguigu.srb.core.pojo.vo.BorrowerDetailVO;
import com.atguigu.srb.core.pojo.vo.BorrowerVO;
import com.atguigu.srb.core.service.BorrowerAttachService;
import com.atguigu.srb.core.service.BorrowerService;
import com.atguigu.srb.core.service.DictService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 借款人 服务实现类
 * </p>
 *
 * @author fun
 * @since 2021-10-29
 */
@Service
public class BorrowerServiceImpl extends ServiceImpl<BorrowerMapper, Borrower> implements BorrowerService {
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    BorrowerAttachMapper borrowerAttachMapper;
    @Autowired
    BorrowerAttachService borrowerAttachService;
    @Autowired
    DictService dictService;
    @Autowired
    UserIntegralMapper userIntegralMapper;

    @Override
    public void saveBorrower(BorrowerVO borrowerVO, Long userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);

        //保存borrower表
        Borrower borrower = new Borrower();
        BeanUtils.copyProperties(borrowerVO, borrower);
        borrower.setUserId(userId);
        borrower.setName(userInfo.getName());
        borrower.setIdCard(userInfo.getIdCard());
        borrower.setMobile(userInfo.getMobile());
        borrower.setStatus(BorrowerStatusEnum.AUTH_RUN.getStatus());
        baseMapper.insert(borrower);

        //生成borrower主键
        Long borrowerId = borrower.getId();

        //保存borrower_attach表
        List<BorrowerAttach> borrowerAttachList = borrowerVO.getBorrowerAttachList();
        for (BorrowerAttach borrowerAttach : borrowerAttachList) {
            borrowerAttach.setBorrowerId(borrowerId);
            borrowerAttachMapper.insert(borrowerAttach);
        }

        //更新会员状态，更新为认证中
        userInfo.setBorrowAuthStatus(BorrowerStatusEnum.AUTH_RUN.getStatus());
        userInfoMapper.updateById(userInfo);


    }

    @Override
    public Integer getBorrowerStatus(Long userId) {
        QueryWrapper<Borrower> borrowerQueryWrapper = new QueryWrapper<>();
        borrowerQueryWrapper.eq("user_id", userId);
        Borrower borrower = baseMapper.selectOne(borrowerQueryWrapper);
        if (borrower == null) {
            //借款人尚未提交信息
            return BorrowerStatusEnum.NO_AUTH.getStatus();
        }
        Integer status = borrower.getStatus();
        return status;
    }

    @Override
    public Page<Borrower> pageList(Page<Borrower> borrowerPage, String keyword) {
        QueryWrapper<Borrower> borrowerQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(keyword)) {
            return baseMapper.selectPage(borrowerPage, null);
        }
        borrowerQueryWrapper
                .like("name", keyword)
                .or()
                .like("mobile", keyword)
                .or()
                .like("id_card", keyword);
        Page<Borrower> selectPage = baseMapper.selectPage(borrowerPage, borrowerQueryWrapper);
        return selectPage;
    }

    @Override
    public BorrowerDetailVO getBorrowerDetailVOById(Long id) {
        //获取借款人信息
        Borrower borrower = baseMapper.selectById(id);
        //填充基本借款人信息
        BorrowerDetailVO borrowerDetailVO = new BorrowerDetailVO();
        BeanUtils.copyProperties(borrower, borrowerDetailVO);

        //婚否
        borrowerDetailVO.setMarry(borrower.getMarry() ? "是" : "否");
        //性别
        borrowerDetailVO.setSex(borrower.getSex() == 1 ? "男" : "女");

        //计算下拉列表选中内容
        String education = dictService.getNameByParentDictCodeAndValue("education", borrower.getEducation());
        String industry = dictService.getNameByParentDictCodeAndValue("industry", borrower.getIndustry());
        String income = dictService.getNameByParentDictCodeAndValue("income", borrower.getIncome());
        String returnSource = dictService.getNameByParentDictCodeAndValue("returnSource", borrower.getReturnSource());
        String contactsRelation = dictService.getNameByParentDictCodeAndValue("relation", borrower.getContactsRelation());

        //设置下拉列表选中内容
        borrowerDetailVO.setEducation(education);
        borrowerDetailVO.setIndustry(industry);
        borrowerDetailVO.setIncome(income);
        borrowerDetailVO.setReturnSource(returnSource);
        borrowerDetailVO.setContactsRelation(contactsRelation);

        //审批状态
        String status = BorrowerStatusEnum.getMsgByStatus(borrower.getStatus());
        borrowerDetailVO.setStatus(status);

        //获取附件VO列表
        List<BorrowerAttachVO> borrowerAttachVOList = borrowerAttachService.BorrowerAttachMapper(id);
        borrowerDetailVO.setBorrowerAttachVOList(borrowerAttachVOList);

        return borrowerDetailVO;
    }

    @Override
    public void approval(BorrowerApprovalVO borrowerApprovalVO) {
        //Borrower 表状态改为已审核
        Borrower borrower = new Borrower();
        borrower.setId(borrowerApprovalVO.getBorrowerId());
        borrower.setStatus(borrowerApprovalVO.getStatus());
        baseMapper.updateById(borrower);

        Long userId = borrowerApprovalVO.getUserId();
        System.out.println(borrowerApprovalVO);

        //user_integral 表插入积分数据
        BigDecimal total = new BigDecimal("0");
        Integer infoIntegral = borrowerApprovalVO.getInfoIntegral();
        if (infoIntegral > 0) {
            // 增加基本积分
            total = total.add(new BigDecimal(infoIntegral));
            UserIntegral userIntegral = new UserIntegral();
            userIntegral.setUserId(userId);
            userIntegral.setContent("info");
            userIntegral.setIntegral(infoIntegral);
            userIntegralMapper.insert(userIntegral);

        }
        // 增加房子积分
        Boolean isHouseOk = borrowerApprovalVO.getIsHouseOk();
        if (isHouseOk) {
            total = total.add(new BigDecimal(IntegralEnum.BORROWER_HOUSE.getIntegral()));
            UserIntegral userIntegral = new UserIntegral();
            userIntegral.setUserId(userId);
            userIntegral.setContent(IntegralEnum.BORROWER_HOUSE.getMsg());
            userIntegral.setIntegral(IntegralEnum.BORROWER_HOUSE.getIntegral());
            userIntegralMapper.insert(userIntegral);
        }
        // 增加房子积分
        Boolean isCarOk = borrowerApprovalVO.getIsCarOk();
        if (isCarOk) {
            total = total.add(new BigDecimal(IntegralEnum.BORROWER_CAR.getIntegral()));
            UserIntegral userIntegral = new UserIntegral();
            userIntegral.setUserId(userId);
            userIntegral.setContent(IntegralEnum.BORROWER_CAR.getMsg());
            userIntegral.setIntegral(IntegralEnum.BORROWER_CAR.getIntegral());
            userIntegralMapper.insert(userIntegral);
        }
        // 增加身份证积分
        Boolean IdCardOk = borrowerApprovalVO.getIsIdCardOk();
        if (IdCardOk) {
            total = total.add(new BigDecimal(IntegralEnum.BORROWER_IDCARD.getIntegral()));
            UserIntegral userIntegral = new UserIntegral();
            userIntegral.setUserId(userId);
            userIntegral.setContent(IntegralEnum.BORROWER_IDCARD.getMsg());
            userIntegral.setIntegral(IntegralEnum.BORROWER_IDCARD.getIntegral());
            userIntegralMapper.insert(userIntegral);
        }

        //UserInfo 表borrow_auth_status状态改为已审核，积分总分
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);
        userInfo.setIntegral(total.intValue());
        userInfo.setBorrowAuthStatus(BorrowerStatusEnum.AUTH_OK.getStatus());
        userInfoMapper.updateById(userInfo);
    }
}
